import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

let router = new Router({
    routes: [
        {
            path: '/',
            redirect: '/dashboard',
            meta: {
                requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
        },
        {
            path: '/',
            component: resolve => require(['../components/common/Home.vue'], resolve),
            meta: {
                title: '自述文件',
                requireAuth: true,  // 添加该字段，表示进入这个路由是需要登录的
            },
            children: [
                {
                    path: '/dashboard',
                    component: resolve => require(['../components/page/Dashboard.vue'], resolve),
                    meta: { title: '系统首页', requireAuth: true}
                },
                {
                    path: '/appManage',
                    component: resolve => require(['../components/page/AppManage.vue'], resolve),
                    meta: { title: 'APP管理', requireAuth: true}
                },
                {
                    path: '/funManage',
                    component: resolve => require(['../components/page/FunManage.vue'], resolve),
                    meta: { title: '业务功能管理', requireAuth: true}
                },
                {
                    path: '/companyList',
                    component: resolve => require(['../components/page/CompanyList.vue'], resolve),
                    meta: { title: '企业列表查看',requireAuth: true }
                },
                {
                    path: '/compFunSet',
                    component: resolve => require(['../components/page/CompFunSet.vue'], resolve),
                    meta: { title: '企业使用列表' }
                },
                {
                    path: '/moduleList',
                    component: resolve => require(['../components/page/ModuleList.vue'], resolve),
                    meta: { title: '功能模块管理' }
                },
                {
                    path: '/message',
                    component: resolve => require(['../components/page/Tabs.vue'], resolve),
                    meta: { title: '系统消息' }
                }, // ---------------------- end --------------------
                {
                    path: '/form',
                    component: resolve => require(['../components/page/BaseForm.vue'], resolve),
                    meta: { title: '基本表单' }
                },
                {
                    // 富文本编辑器组件
                    path: '/editor',
                    component: resolve => require(['../components/page/VueEditor.vue'], resolve),
                    meta: { title: '富文本编辑器' }
                },
                {
                    // markdown组件
                    path: '/markdown',
                    component: resolve => require(['../components/page/Markdown.vue'], resolve),
                    meta: { title: 'markdown编辑器' }
                },
                {
                    // 图片上传组件
                    path: '/upload',
                    component: resolve => require(['../components/page/Upload.vue'], resolve),
                    meta: { title: '文件上传' }
                },
                {
                    // vue-schart组件
                    path: '/charts',
                    component: resolve => require(['../components/page/BaseCharts.vue'], resolve),
                    meta: { title: 'schart图表' }
                },
                {
                    // 拖拽列表组件
                    path: '/drag',
                    component: resolve => require(['../components/page/DragList.vue'], resolve),
                    meta: { title: '拖拽列表' }
                },
                {
                    // 权限页面
                    path: '/permission',
                    component: resolve => require(['../components/page/Permission.vue'], resolve),
                    meta: { title: '权限测试', permission: true }
                }
            ]
        },
        {
            path: '/login',
            component: resolve => require(['../components/page/Login.vue'], resolve)
        },
        {
            path: '/404',
            component: resolve => require(['../components/page/404.vue'], resolve)
        },
        {
            path: '/403',
            component: resolve => require(['../components/page/403.vue'], resolve)
        },
        {
            path: '*',
            redirect: '/appManage'
        }
    ]
})

router.beforeEach((to, from, next) => {
    let token = localStorage.getItem('token')
    if (to.meta.requireAuth) {
        if (token) { //通过vuex state获取当前的token是否存在
            next();
        } else {
            next('/login')
        }
    } else {
        next();
    }
});

export default router